package cn.stylefeng.guns.modular.rw.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.core.ParamUtil;
import cn.stylefeng.guns.modular.rw.entity.MerToJoin;
import cn.stylefeng.guns.modular.rw.service.IMerToJoinService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 描述：招商加盟表 controller
 * 作者：chenhuiyan
 * 时间：2020-05-07
 */
@Controller
@RequestMapping("/rw/merToJoin")
public class MerToJoinController extends BaseController {

    private String PREFIX = "/modular/rw/merToJoin/";

    @Autowired
    private IMerToJoinService merToJoinService;

    /**
     * 跳转到管理首页
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX + "merToJoin.html";
    }

    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Object list() {
        Map<String, Object> params = ParamUtil.getParams();
        Page<Map<String, Object>> list = merToJoinService.list(params);
        return LayuiPageFactory.createPageInfo(list);
    }

    /**
     * 跳转添加/修改页面
     * @param model
     * @param merToJoinId
     * @return
     */
    @RequestMapping(value = "/addOrEdit")
    public String addOrEdit(Model model, String merToJoinId) {
        model.addAttribute("merToJoinId", merToJoinId);
        return PREFIX + "merToJoin_add.html";
    }

    /**
     * 保存
     * @param merToJoin
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public ResponseData save(MerToJoin merToJoin) {
        if (ToolUtil.isEmpty(merToJoin.getId())) {
            merToJoin.preInsert();
            merToJoinService.save(merToJoin);
        }else{
            merToJoin.preUpdate();
            merToJoinService.updateById(merToJoin);
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取详情
     * @param merToJoinId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getInfoById/{merToJoinId}")
    public Object getInfoById(@PathVariable("merToJoinId") String merToJoinId){
        Map<String, Object> res = merToJoinService.getInfoById(merToJoinId);
        return res;
    }

    /**
     * 删除（物理删除）
     * @param merToJoinId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete")
    public ResponseData delete(@RequestParam(name = "merToJoinId") String merToJoinId){
        if(ToolUtil.isEmpty(merToJoinId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        merToJoinService.removeById(merToJoinId);
        return SUCCESS_TIP;
    }

    /**
     * 修改状态
     * @param merToJoinId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatus")
    public ResponseData updateStatus(@RequestParam(name = "merToJoinId") String merToJoinId, @RequestParam(name = "status") Integer status){
        if(ToolUtil.isEmpty(merToJoinId)){
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        MerToJoin merToJoin = merToJoinService.getById(merToJoinId);
        merToJoin.setStatus(status);
        merToJoin.preUpdate();
        return SUCCESS_TIP;
    }

}
